﻿using System;
using System.Collections.Generic;
using System.Text;
using HundredMilesSoftware.UltraID3Lib;
using System.Reflection;
using System.IO;

namespace GotLyrics
{
    public class Tags
    {
        private UltraID3 _id3;
        /// <summary>
        /// Gets the UltraID3 instance.
        /// </summary>
        /// <value>The UltraID3 instance.</value>
        internal UltraID3 ID3
        {
            get { return _id3; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Tags"/> class.
        /// </summary>
        /// <param name="id3">The id3.</param>
        internal Tags(UltraID3 id3)
        {
            _id3 = id3;
        }

        /// <summary>
        /// Gets the <see cref="System.Object"/> with the specified field.
        /// </summary>
        /// <value></value>
        public object this[string field]
        {
            get
            {
                PropertyInfo propInfo = ID3.GetType().GetProperty(field);

                if (propInfo == null)
                    return null;

                return propInfo.GetValue(ID3, null);
            }
        }

        /// <summary>
        /// Gets or sets the album.
        /// </summary>
        /// <value>The album.</value>
        public string Album
        {
            get { return _id3.Album; }
            set { _id3.Album = value; }
        }

        /// <summary>
        /// Gets or sets the artist.
        /// </summary>
        /// <value>The artist.</value>
        public string Artist
        {
            get { return _id3.Artist; }
            set { _id3.Artist = value; }
        }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title
        {
            get { return _id3.Title; }
            set { _id3.Title = value; }
        }

        /// <summary>
        /// Gets or sets the track number.
        /// </summary>
        /// <value>The track number.</value>
        public short? TrackNum
        {
            get { return _id3.TrackNum; }
            set { _id3.TrackNum = value; }
        }

        /// <summary>
        /// Gets or sets the track count.
        /// </summary>
        /// <value>The track count.</value>
        public short? TrackCount
        {
            get {  return _id3.TrackCount; }
            //set { _id3.TrackCount = value; }
        }

        /// <summary>
        /// Gets or sets the year.
        /// </summary>
        /// <value>The year.</value>
        public short? Year
        {
            get { return _id3.Year; }
            set { _id3.Year = value; }
        }

        /// <summary>
        /// Gets the size.
        /// </summary>
        /// <value>The size.</value>
        public long? Size
        {
            get { return _id3.Size; }
        }

        /// <summary>
        /// Gets or sets the comments.
        /// </summary>
        /// <value>The comments.</value>
        public string Comments
        {
            get { return _id3.Comments; }
            set { _id3.Comments = value; }
        }

        /// <summary>
        /// Gets the duration.
        /// </summary>
        /// <value>The duration.</value>
        public TimeSpan Duration
        {
            get { return _id3.Duration; }
        }

        /// <summary>
        /// Gets or sets the unsynced lyrics.
        /// </summary>
        /// <value>The unsynced lyrics.</value>
        public string UnsyncedLyrics
        {
            get 
            {
                ID3FrameCollection frames = _id3.ID3v2Tag.Frames.GetFrames(MultipleInstanceID3v2FrameTypes.ID3v23UnsyncedLyrics);

                if (frames != null && frames.Count > 0)
                    return ((ID3v23UnsynchedLyricsFrame)frames[0]).UnsynchedLyrics;
                
                return null;
            }
            set
            {
                _id3.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23UnsyncedLyrics);

                if (!string.IsNullOrEmpty(value))
                {
                    //string d = MinifyString(value);
                    ID3v23UnsynchedLyricsFrame frame = new ID3v23UnsynchedLyricsFrame(TextEncodingTypes.ISO88591);
                    frame.UnsynchedLyrics = value;
                    _id3.ID3v2Tag.Frames.Add(frame);
                }
            }
        }
    }
}
